package com.zhiyou100.video.dao.impl;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;

import com.zhiyou100.video.dao.UserDao;
import com.zhiyou100.video.model.User;
@SuppressWarnings("all")
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {

	@Override
	public void addRegister(User upe) {
		getHibernateTemplate().saveOrUpdate(upe);
	}

	
	@Override
	public List<User> selectUser(DetachedCriteria dc) {
		
		return (List<User>) getHibernateTemplate().findByCriteria(dc);
	}


	@Override
	public void updateUserProfile(User uer) {
		User user = getHibernateTemplate().get(User.class, uer.getId());
		user.setNickName(uer.getNickName());
		user.setBirthday(uer.getBirthday());
		user.setSex(uer.getSex());
		user.setCity(uer.getCity());
		user.setProvince(uer.getProvince());
	}


	@Override
	public User selectUserSession(User uer) {
		return getHibernateTemplate().get(User.class, uer.getId());
	}


	@Override
	public void updateUserAvatar(User uer) {
		User user = getHibernateTemplate().get(User.class, uer.getId());
		user.setHeadUrl(uer.getHeadUrl());
	}


	@Override
	public void updatePassWord(User upe) {
		User user = getHibernateTemplate().get(User.class, upe.getId());
		user.setPassword(upe.getPassword());
	}


	@Override
	public void updateSendemail(User upe) {
		DetachedCriteria dc = DetachedCriteria.forClass(User.class);
		dc.add(Restrictions.eq("email", upe.getEmail()));
		 List<User> list = (List<User>) getHibernateTemplate().findByCriteria(dc);
		 User user = list.get(0);
		user.setCaptcha(upe.getCaptcha());
	}


	@Override
	public void updateResetPwd(User uer) {
		User user = getHibernateTemplate().get(User.class, uer.getId());
		user.setPassword(uer.getPassword());
		user.setCaptcha("");
	}

	

}
